import { useI18n } from '@/hooks/web/useI18n'
// import axios from 'axios'
import { Dialog } from 'vant'

const settings = {
  // 抓取 hash 的正则
  hashReg: /app\.([^.]+)\.js/,

  // 抓取新的 index.html 内容
  getNewHtmlContent() {
    // if (process.env.NODE_ENV !== 'production') {
    //   return new Promise((resolve, reject) => {
    //     resolve(
    //       `
    // <!DOCTYPE html><html><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1"><meta name=renderer content=webkit><meta name=viewport content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><link rel=icon href=/favicon.ico><title>又赚啦</title><link href=/static/js/app.b9b50f2f2.js rel=preload as=script><link href=/static/js/chunk-antUI.d761a650.js rel=preload as=script><link href=/static/js/chunk-elementUI.e20084f1.js rel=preload as=script></head><body><div id=app></div><script>var _hmt = _hmt || [];
    //   (function() {
    //     var hm = document.createElement("script");
    //     hm.src = "https://hm.baidu.com/hm.js?d3e1c169ebd76229c404508fcd354653";
    //     var s = document.getElementsByTagName("script")[0];
    //     s.parentNode.insertBefore(hm, s);
    //   })();</script><script src=/static/js/chunk-elementUI.e20084f1.js></script><script src=/static/js/chunk-antUI.d761a650.js></script><script src=/static/js/app.b9b50f2f2.js></script></body></html>
    // `,
    //     )
    //   })
    // }

    return fetch('/?=t' + Date.now()).then((response) => response.text())

    // return axios
    //   .get('/index.html?=t' + Date.now())
    //   .then(({ data }: { data: string }) => {
    //     return data
    //   })
  },

  // 有更新情况执行
  withUpdateCallback() {
    let { t } = useI18n()
    Dialog.confirm({
      title: t('common.pageHasUpdatedTitle'),
      message: t('common.pageHasUpdatedMsg'),
      confirmButtonText: t('common.okText'),
      cancelButtonText: t('common.cancelText'),
    }).then(() => {
      location.reload(true)
    })
  },
}

interface settings {
  hashReg: RegExp
  getNewHtmlContent: () => Promise<string>
  withUpdateCallback: () => void
}

function checkUpdate(settings: settings) {
  const { hashReg, getNewHtmlContent, withUpdateCallback } = settings

  function getHash(str: string) {
    const res = str.match(hashReg)
    if (res) {
      return res[1]
    }
    return null
  }
  function getOldHash(cb: (hash: string) => void) {
    const scripts = document.getElementsByTagName('script')

    // 模拟测试
    // if (process.env.NODE_ENV !== 'production') {
    //   scripts = [...scripts, { src: 'js/app.b9b50f2f.js' }]
    // }

    for (let index = 0, length = scripts.length; index < length; index++) {
      const script = scripts[index]
      const hash = getHash(script.src)
      if (hash) {
        cb(hash)
        break
      }
    }
  }

  getOldHash(async (hash: string) => {
    const newHtmlContent = await getNewHtmlContent()
    const newHash = getHash(newHtmlContent)
    if (newHash && newHash !== hash) {
      withUpdateCallback()
    }
  })
}

export function startCheckUpdate() {
  setTimeout(() => {
    checkUpdate(settings)
  }, 10)
}
